using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qWBDAT = from m in mtemp.WBDAT select m;
       DBMapper.MapWBDAT(qWBDAT.ToList(), true);
       */
        public List<WBDAT> MapWBDAT(XElement item2, bool mergedb)
        {
            List<WBDAT> result = new List<WBDAT>();
            var item4 = from item3 in item2.Elements()
                        select new WBDAT
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            WBMANDANT = (string)item3.Element("WBMANDANT"),
                            WBRWKRS = (string)item3.Element("WBRWKRS"),
                            WBRWLFD = (string)item3.Element("WBRWLFD"),
                            WBWAKZ = (string)item3.Element("WBWAKZ"),
                            WBART = (string)item3.Element("WBART"),
                            WBVAR = (string)item3.Element("WBVAR"),
                            WBCOLORIT = (string)item3.Element("WBCOLORIT"),
                            WBFARBE = (string)item3.Element("WBFARBE"),
                            WBBESCHICH = (string)item3.Element("WBBESCHICH"),
                            WBZUSTAND = (string)item3.Element("WBZUSTAND"),
                            WBEINLO = (string)item3.Element("WBEINLO"),
                            WBSTK = (string)item3.Element("WBSTK"),
                            WBMTR = (string)item3.Element("WBMTR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapWBDAT(result, mergedb);
          return result;
        }
        ///DBMapper.MapWBDAT(mtemp.WBDAT.ToList<WBDAT>(), true);        
       public void MapWBDAT(List<WBDAT> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from WBDAT row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.WBDAT.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.WBDAT.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.WBDAT.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "WBDAT")]
    public class WBDAT : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _WBMANDANT="0";
        [Column]
        public string WBMANDANT
        {
            get
            {
                return _WBMANDANT;
            }
            set
            {
                if (value != _WBMANDANT)
                {
                     _WBMANDANT = value;
                    NotifyPropertyChanged("WBMANDANT");
                }
            }
        }
        private string  _WBRWKRS="0";
        [Column]
        public string WBRWKRS
        {
            get
            {
                return _WBRWKRS;
            }
            set
            {
                if (value != _WBRWKRS)
                {
                     _WBRWKRS = value;
                    NotifyPropertyChanged("WBRWKRS");
                }
            }
        }
        private string  _WBRWLFD="0";
        [Column]
        public string WBRWLFD
        {
            get
            {
                return _WBRWLFD;
            }
            set
            {
                if (value != _WBRWLFD)
                {
                     _WBRWLFD = value;
                    NotifyPropertyChanged("WBRWLFD");
                }
            }
        }
        private string  _WBWAKZ="0";
        [Column]
        public string WBWAKZ
        {
            get
            {
                return _WBWAKZ;
            }
            set
            {
                if (value != _WBWAKZ)
                {
                     _WBWAKZ = value;
                    NotifyPropertyChanged("WBWAKZ");
                }
            }
        }
        private string  _WBART="0";
        [Column]
        public string WBART
        {
            get
            {
                return _WBART;
            }
            set
            {
                if (value != _WBART)
                {
                     _WBART = value;
                    NotifyPropertyChanged("WBART");
                }
            }
        }
        private string  _WBVAR="0";
        [Column]
        public string WBVAR
        {
            get
            {
                return _WBVAR;
            }
            set
            {
                if (value != _WBVAR)
                {
                     _WBVAR = value;
                    NotifyPropertyChanged("WBVAR");
                }
            }
        }
        private string  _WBCOLORIT="0";
        [Column]
        public string WBCOLORIT
        {
            get
            {
                return _WBCOLORIT;
            }
            set
            {
                if (value != _WBCOLORIT)
                {
                     _WBCOLORIT = value;
                    NotifyPropertyChanged("WBCOLORIT");
                }
            }
        }
        private string  _WBFARBE="0";
        [Column]
        public string WBFARBE
        {
            get
            {
                return _WBFARBE;
            }
            set
            {
                if (value != _WBFARBE)
                {
                     _WBFARBE = value;
                    NotifyPropertyChanged("WBFARBE");
                }
            }
        }
        private string  _WBBESCHICH="0";
        [Column]
        public string WBBESCHICH
        {
            get
            {
                return _WBBESCHICH;
            }
            set
            {
                if (value != _WBBESCHICH)
                {
                     _WBBESCHICH = value;
                    NotifyPropertyChanged("WBBESCHICH");
                }
            }
        }
        private string  _WBZUSTAND="0";
        [Column]
        public string WBZUSTAND
        {
            get
            {
                return _WBZUSTAND;
            }
            set
            {
                if (value != _WBZUSTAND)
                {
                     _WBZUSTAND = value;
                    NotifyPropertyChanged("WBZUSTAND");
                }
            }
        }
        private string  _WBEINLO="0";
        [Column]
        public string WBEINLO
        {
            get
            {
                return _WBEINLO;
            }
            set
            {
                if (value != _WBEINLO)
                {
                     _WBEINLO = value;
                    NotifyPropertyChanged("WBEINLO");
                }
            }
        }
        private string  _WBSTK="0";
        [Column]
        public string WBSTK
        {
            get
            {
                return _WBSTK;
            }
            set
            {
                if (value != _WBSTK)
                {
                     _WBSTK = value;
                    NotifyPropertyChanged("WBSTK");
                }
            }
        }
        private string  _WBMTR="0";
        [Column]
        public string WBMTR
        {
            get
            {
                return _WBMTR;
            }
            set
            {
                if (value != _WBMTR)
                {
                     _WBMTR = value;
                    NotifyPropertyChanged("WBMTR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<WBDAT> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\WBDAT\\WBDAT" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\WBDAT" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("WBDAT");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (WBDAT row in ListInput)
                {
                    XElement f = new XElement("WBDAT");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement WBMANDANT = new XElement("WBMANDANT");
                         WBMANDANT.Value = row.WBMANDANT;
                          f.Add(WBMANDANT);
                        XElement WBRWKRS = new XElement("WBRWKRS");
                         WBRWKRS.Value = row.WBRWKRS;
                          f.Add(WBRWKRS);
                        XElement WBRWLFD = new XElement("WBRWLFD");
                         WBRWLFD.Value = row.WBRWLFD;
                          f.Add(WBRWLFD);
                        XElement WBWAKZ = new XElement("WBWAKZ");
                         WBWAKZ.Value = row.WBWAKZ;
                          f.Add(WBWAKZ);
                        XElement WBART = new XElement("WBART");
                         WBART.Value = row.WBART;
                          f.Add(WBART);
                        XElement WBVAR = new XElement("WBVAR");
                         WBVAR.Value = row.WBVAR;
                          f.Add(WBVAR);
                        XElement WBCOLORIT = new XElement("WBCOLORIT");
                         WBCOLORIT.Value = row.WBCOLORIT;
                          f.Add(WBCOLORIT);
                        XElement WBFARBE = new XElement("WBFARBE");
                         WBFARBE.Value = row.WBFARBE;
                          f.Add(WBFARBE);
                        XElement WBBESCHICH = new XElement("WBBESCHICH");
                         WBBESCHICH.Value = row.WBBESCHICH;
                          f.Add(WBBESCHICH);
                        XElement WBZUSTAND = new XElement("WBZUSTAND");
                         WBZUSTAND.Value = row.WBZUSTAND;
                          f.Add(WBZUSTAND);
                        XElement WBEINLO = new XElement("WBEINLO");
                         WBEINLO.Value = row.WBEINLO;
                          f.Add(WBEINLO);
                        XElement WBSTK = new XElement("WBSTK");
                         WBSTK.Value = row.WBSTK;
                          f.Add(WBSTK);
                        XElement WBMTR = new XElement("WBMTR");
                         WBMTR.Value = row.WBMTR;
                          f.Add(WBMTR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
